/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.socialNet.commandBeans;

import com.bionic.socialNet.dao.interfaces.IProfileDAO;
import com.bionic.socialNet.dao.interfaces.IUserDAO;
import com.bionic.socialNet.db.DB;
import com.bionic.socialNet.entities.LoginData;
import com.bionic.socialNet.entities.ProfileData;
import com.bionic.socialNet.entities.UserData;
import java.util.ArrayList;

/**
 *
 * @author Ann
 *
 */
public class UserBean {

    private UserData currentUser;
    private ProfileData userProfile;
    private IUserDAO userDAO;
    private IProfileDAO profileDAO;

    public UserBean() {
        userDAO = DB.getInstance().getFactory().getUserDAO();
        profileDAO = DB.getInstance().getFactory().getProfileDAO();

    }

    // register new user: check if name/email is unique then register new user 
    public boolean isRegisterNewUser(LoginData loginData) {
        boolean isNewUserCreate = userDAO.registerUser(loginData);
        if (isNewUserCreate) {
            setCurrentUser(loginData);
        }
        return isNewUserCreate;
    }

    /**
     * @return the currentUser
     */
    public UserData getCurrentUser() {
        return currentUser;
    }

    /**
     * @param loginData data to find CurrentUSer
     */
    private void setCurrentUser(LoginData loginData) {
        this.currentUser = userDAO.findUser(loginData.getLogin());
    }

    public boolean canCreateUserWithLogin(String login) {
        return !(userDAO.isLoginAlreadyTaken(login));
    }

    public boolean isLogin(LoginData loginData) {
        boolean isLogin = false;
        isLogin = userDAO.isLoginExecute(loginData);
        if (isLogin) {
            setCurrentUser(loginData);
            FindProfile(getCurrentUser().getUserID());
        }
        return isLogin;

    }

    public ProfileData getUserProfile() {
        return userProfile;
    }

    /**
     * @param profile the profile to set
     */
    private void FindProfile(int userID) {
        // findProfile
        this.userProfile = profileDAO.findProfile(userID);
    }

    public ProfileData findCommonProfile(int userID) {
        return profileDAO.findProfile(userID);
    }

    public ArrayList<ProfileData> findProfiles(String userName) {
        String userNameLike = "%" + userName + "%";
        return (ArrayList<ProfileData>) profileDAO.selectProfileDataCollection(userNameLike);
    }
}
